package org.footito.android.ui.adapters;

import java.util.ArrayList;

import org.footito.android.R;
import org.footito.android.data.Comment;
import org.footito.android.tools.Log;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentsBaseAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ArrayList<Comment> list;

	public CommentsBaseAdapter(Context context, ArrayList<Comment> commentsList) {
		inflater = LayoutInflater.from(context);
		this.list = commentsList;
	}

	@Override
	public int getCount() {
		if (list.size() < 1)
			return 1;
		else
			return list.size();
	}

	@Override
	public Object getItem(int location) {
		if (list.size() < 1)
			return null;
		else
			return list.get(location);
	}

	@Override
	public long getItemId(int location) {
		return location;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.row_comment_with_flag, null);
		}

		if (convertView.getVisibility() == View.GONE)
			convertView.setVisibility(View.VISIBLE);

		try {
			TextView comment = (TextView) convertView.findViewById(R.id.TV_Comment);
			ImageView flag = (ImageView) convertView.findViewById(R.id.IV_Flag);

			if (list.size() < 1) {
//				comment.setText(parent.getContext().getResources().getString(R.string.no_comment));
				comment.setText("pas de comments"); // FIXME
				flag.setVisibility(View.GONE);
			} else {
				Comment current = list.get(position);
				comment.setText(Html.fromHtml("<b>" + current.getTeam().getName() + "</b> : " + current.getContent()));
				Bitmap bitmap = current.getTeam().getPicture(parent.getContext());
				flag.setImageBitmap(bitmap);
				flag.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
			Log.e("Exception: " + e.getClass().toString() + ": " + e.getMessage());
			convertView.setVisibility(View.GONE);
		}

		return convertView;
	}

}
